<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('设备修改')" />
    <th:block th:include="include :: datetimepicker-css" />
    <style type="text/css">
        table label.error{position: inherit;}select + label.error{z-index:1;right:40px;}
    </style>
</head>
<body class="gray-bg">
<div class="main-content">
    <form class="form-horizontal m" id="form-device-edit" th:object="${myDevice}">
        <input name="id" th:field="*{id}" type="hidden">
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">设备名称：</label>
                    <div class="col-sm-8">
                        <input name="name" th:field="*{name}" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">设备串口：</label>
                    <div class="col-sm-8">
                        <input name="serialPort" th:field="*{serialPort}" class="form-control" type="text">
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">比特率：</label>
                    <div class="col-sm-8">
                        <input name="bitRate" th:field="*{bitRate}" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">设备运行状态：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <!--<input name="state" th:field="*{state}" class="form-control" type="text">-->
                            <select class='form-control' name='state' th:field="*{state}">
                                <option value="1" {{if state===1}}selected{{/if}}>已停止</option>
                                <option value="2" {{if state===2}}selected{{/if}}>正在运行</option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">设备运行时间：</label>
                    <div class="col-sm-8"><!--th:value="${#dates.format(myDevice.runTime, 'yyyy-MM-dd')}"-->
                        <input name="runTime" th:value="*{runTime}" class="form-control" placeholder="yyyy-MM-dd" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">备注：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <textarea name="remark" class="form-control">[[*{remark}]]</textarea>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <h4 class="form-header h4">设备明细数据</h4>
        <div class="row">
            <div class="col-sm-12">
                <button type="button" class="btn btn-white btn-sm" onclick="addRow()"><i class="fa fa-plus"> 增加</i></button>
                <button type="button" class="btn btn-white btn-sm" onclick="sub.delRow()"><i class="fa fa-minus"> 删除</i></button>
                <div class="col-sm-12 select-table table-striped">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </form>
</div>

<div class="row">
    <div class="col-sm-offset-5 col-sm-10">
        <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
        <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
    </div>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<script th:src="@{/js/jquery.tmpl.js}"></script>
<script th:inline="javascript">
    var prefix = ctx + "system/device";
    var detail_prefix = ctx + "system/details";
    $("#form-device-edit").validate({
        focusCleanup: true
    });

    /*function submitHandler() {;
        if ($.validate.form()) {
            $.operate.save(prefix + "/edit", $('#form-device-edit').serialize());
        }
    }*/

    $(function() {
        console.log(JSON.stringify(id.defaultValue))
        // 初始化数据, 可以由后台传过来
        var data = [
            {
                id: "100",
                name: "商品名称",
                weight: "100",
                price: "12.5",
                date: "2021-02-01",
                type: "1",
            },
            {
                id: "101",
                name: "商品名称2",
                weight: "50",
                price: "10.8",
                date: "2021-02-01",
                type: "0",
            }];
        var options = {
            //data: data,
            url: detail_prefix + "/list",
            queryParams : {
                deviceId: id.defaultValue
            },
            pagination: false,
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            sidePagination: "client",
            columns: [{
                checkbox: true
            },
                /*{
                    field: 'id',
                    title: '明细id',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].id' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'deviceId',
                    title: '设备id',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].deviceId' value='%s'>", index, value);
                        return html;
                    }
                },*/
                {
                    field: 'p1Now',
                    title: 'CGMS机器当前的真空度',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].p1Now' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'p2Now',
                    title: '样品舱当前的真空度',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].p2Now' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'p2Seting',
                    title: '样品舱真空度的设定值',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].p2Seting' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'pt1Seting',
                    title: '舱体升温速率设定值',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].pt1Seting' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'cangtiH11Now',
                    title: '舱体当前温度h1',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].cangtiH11Now' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'cangtiH12Now',
                    title: '样品舱当前温度h2',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].cangtiH12Now' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'cangtiT1Seting',
                    title: '舱体设定加热温度设定值',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].cangtiT1Seting' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'cangtiT0Seting',
                    title: '舱体起始温度设定值',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].cangtiT0Seting' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'bujijingQH1Now',
                    title: '捕集阱前当前温度',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].bujijingQH1Now' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'bujijingT2Now',
                    title: '捕集阱温度 低温设定值',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].bujijingT2Now' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'bujijingT2Seting',
                    title: '捕集阱温度 高温当前温度值',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].bujijingT2Seting' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'bujijingT3Now',
                    title: '捕集阱温度 高温当前温度值',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].bujijingT3Now' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'bujijingT3Seting',
                    title: '捕集阱温度 高温设定值',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].bujijingT3Seting' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'bujijingT41Now',
                    title: '捕集阱T4-1当前温度',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].bujijingT41Now' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'bujijingT41Seting',
                    title: '捕集阱传输线温度设定值',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].bujijingT41Seting' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'bujijingT42Now',
                    title: '捕集阱T4-2当前温度',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].bujijingT42Now' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'bujijingT42Seting',
                    title: '捕集阱传输线温度设定值',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].bujijingT42Seting' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 't5Now',
                    title: '自动四通选择阀当前温度',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].t5Now' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 't5Seting',
                    title: '自动四通选择阀设定值',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].t5Seting' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'cangtiC1Seting',
                    title: '舱体真空稳定时间设定值',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].cangtiC1Seting' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'cangtiC2Seting',
                    title: '舱体恒温烘烤时间设定值',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].cangtiC2Seting' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'bujijingC3Seting',
                    title: '捕集阱富集时间设定值',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].bujijingC3Seting' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'bujijingC4Seting',
                    title: '捕集阱高温维持时间设定值',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].bujijingC4Seting' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'nowmod',
                    title: '当前模式  0 1 2 3分别对应4种工作模式',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].nowmod' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'runTime',
                    title: '运行时间',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].runTime' value='%s' placeholder='yyyy-MM-dd'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'state',
                    title: '设备运行状态',
                    formatter: function(value, row, index) {
                        /*var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].state' value='%s'>", index, value);
                        return html;*/
                        var data = [{ index: index, type: value }];
                        return $("#stateTpl").tmpl(data).html();
                    }
                },
                {
                    field: 'remark',
                    title: '备注',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control goodsName' type='text' name='detailsList[%s].remark' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var value = $.common.isNotEmpty(row.index) ? row.index : $.table.serialNumber(index);
                        return '<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="sub.delRowByIndex(\'' + value + '\')"><i class="fa fa-remove"></i>删除</a>';
                    }
                }]
        };
        $.table.init(options);
    });

  /*  $.validator.addMethod("goodsName", function(value, element) {
        return !this.optional(element);
    }, "商品名称必填。");

    $.validator.addMethod("goodsWeight", function(value, element) {
        return !this.optional(element) && (value <= 100 && value >= 0);
    }, "商品重量长度区间0-100。");*/

    /* 主子表-提交 */
    function submitHandler(index, layero){
        if ($.validate.form()) {
           /* var data = $("#form-add").serializeArray();
            alert(JSON.stringify(data))
            $.operate.saveModal("/demo/operate/customer/add", data);*/

            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-device-edit').serialize());
            }
        }
    }

    $("input[name='birthday']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    function addRow() {
        var count = $("#" + table.options.id).bootstrapTable('getData').length;
        var row = {
            /*index: $.table.serialNumber(count),*/
            id: "",
            deviceId: "",
            p1Now: "",
            p2Now: "",
            p2Seting: "",
            pt1Seting: "",
            cangtiH11Now:"",
            cangtiH12Now:"",
            cangtiT1Seting:"",
            cangtiT0Seting:"",
            bujijingQH1Now:"",
            bujijingT2Now:"",
            bujijingT2Seting:"",
            bujijingT3Now:"",
            bujijingT3Seting:"",
            bujijingT41Now:"",
            bujijingT41Seting:"",
            bujijingT42Now:"",
            bujijingT42Seting:"",
            t5Now:"",
            t5Seting:"",
            cangtiC1Seting:"",
            cangtiC2Seting:"",
            bujijingC3Seting:"",
            bujijingC4Seting:"",
            nowmod:"",
            runTime:"",
            state:"",
            delFlag:"",
            remark:""
        }
        sub.addRow(row);
    }

    $("#bootstrap-table").on("post-body.bs.table", function (e, args) {
        $("input[name$='runTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true,
            pickerPosition:'top-right'
        });
    });

    $("input[name='runTime']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });
</script>
</body>
</html>

<!-- 商品类型 -->
<script id="stateTpl" type="text/x-jquery-tmpl">
    <div>
    <select class='form-control' name='detailsList[${index}].state'>
        <option value="1" {{if type===1}}selected{{/if}}>已停止</option>
        <option value="2" {{if type===2}}selected{{/if}}>正在运行</option>
    </select>
    </div>
</script>